package com.cskaoyan.gateway.controller.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.pay.PayForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.dto.PayOrderSuccessRequest;
import com.mall.pay.PayCoreService;
import com.mall.pay.constants.PayReturnCodeEnum;
import com.mall.pay.dto.PayQueryRequest;
import com.mall.pay.dto.PaymentRequest;
import com.mall.pay.dto.alipay.AlipayQueryRetResponse;
import com.mall.pay.dto.alipay.AlipaymentResponse;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/*
 * @description: 处理支付的相关请求
 */
@Slf4j
@RestController
@RequestMapping("/cashier")
public class PaymentController {

    //支付核心服务
    @Reference(timeout = 3000, retries = 0, check = false)
    private PayCoreService payCoreService;

    /*
     * @description: 获取支付二维码的静态资源链接
     * @return: com.mall.commons.result.ResponseData
     * @author: mabinbin
     * @date: 2021/8/29
     */
    @PostMapping("/pay")
    public ResponseData pay(@RequestBody PayForm payForm, HttpServletRequest servletRequest) {
        //封装请求
        PaymentRequest paymentRequest = new PaymentRequest();
        paymentRequest.setOrderFee(payForm.getMoney());
        paymentRequest.setTotalFee(payForm.getMoney());
        paymentRequest.setSubject(payForm.getInfo());
        paymentRequest.setTradeNo(payForm.getOrderId());
        paymentRequest.setPayChannel(payForm.getPayType());
        //封装用户id
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        paymentRequest.setUserId(uid);

        AlipaymentResponse response = payCoreService.aliPay(paymentRequest);
        if (response.getCode().equals(PayReturnCodeEnum.SUCCESS.getCode())) {
            String codeUrl = "http://localhost:8080/image/" + response.getQrCode();
            return new ResponseUtil().setData(codeUrl);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /*
     * @description: 查询支付状态
     * @return: com.mall.commons.result.ResponseData
     * @author: mabinbin
     * @date: 2021/8/29
     */
    @GetMapping("/queryStatus")
    public ResponseData queryStatus(String orderId, HttpServletRequest servletRequest) {
        PaymentRequest paymentRequest = new PaymentRequest();
        paymentRequest.setTradeNo(orderId);
        //封装请求
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        paymentRequest.setUserId(uid);

        //查询订单状态
        AlipayQueryRetResponse response = payCoreService.queryAlipayRet(paymentRequest);
        if (response.getCode().equals(PayReturnCodeEnum.SUCCESS.getCode())) {
            return new ResponseUtil().setData(null);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
